using CfgTable;
using IQIGame.Onigao.GamePlay.GamePlay.Runtime.Util;
using UnityEngine;

namespace IQIGame.Onigao.GamePlay
{
    /// <summary>
    /// 控制权限 确保只有 UIMapManager 可以直接创建SymbolData实例
    /// 其它类型只能通过IMapSymbolData访问SymbolData实例
    /// 降低错误使用对象池的可能性
    /// </summary>
    public partial class UIMapManager
    {
        protected class EntityMapSymbolData : IMapSymbolData
        {
            private Transform entityTrans => _entity == null ? null : (_entity.transform == null ? null : (Transform)_entity.transform);

            public int entityId { get; private set; }
            public int mcid { get; private set; }
            public bool isOfferingQuest => _entity == null ? false : _entity.IsOfferingQuest();
            public CfgMapUiEntity config { get; set; }
            public Quaternion worldRotation => entityTrans == null ? Quaternion.identity : _entity.transform.rotation;
            public Vector3 worldPosition => entityTrans == null ? Vector3.zero : _entity.transform.position;

            public void SetRawData(object data)
            {
                if (data is not TViewEntity entity)
                {
                    return;
                }

                entityId = entity.id;
                mcid = entity.mcid;
                var mapUICid = entity.mapSymbolCid;
                config = mapUICid == 0 ? null : TableCenter.mapUiEntity.Get(mapUICid);
                isCheckPointActive = GameAreaAccessUtil.curAreaAccessor.GetCheckPointActive(entity);
                layer = (int)(config?.StaticLayer ?? StaticLayerType.None);
                _entity = entity;
            }

            public object GetRawData()
            {
                return _entity;
            }

            public bool isCheckPointActive { get; private set; }
            public int layer { get; set; }

            public uint MaxStore => 30;

            public void OnReset()
            {
                _entity = null;
                config = null;
                entityId = 0;
                mcid = 0;
                layer = 0;
            }

            private TViewEntity _entity;
        }
    }
}